#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void swap(char* e1, char* e2, size_t width)
{
	char tmp = 0;
	size_t i = 0;
	for (i = 0; i < width; i++)
	{
		tmp = e1[i];
		e1[i] = e2[i];
		e2[i] = tmp;
	}
}
void my_qsort(void* base, size_t num, size_t width, int(*cmp)(void*, void*))
{
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
		int j = 0;
		for (j = 0; j < num - 1 - i; j++)
		{
			if (cmp((char*)base + j * width, (char*)base + width + j * width) > 0)
			{
				swap((char*)base + j * width, (char*)base + width + j * width, width);
			}
		}
	}
}

struct Stu
{
	char name[20];
	int age;
};
int cmp_int(const void* e1, const void* e2)
{
	return *((int*)e1) - *((int*)e2);
}
int cmp_structage(void* e1, void* e2)
{
	return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
int cmp_structname(void* e1, void* e2)
{
	return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}

void test1()
{
	int arr[10] = { 1,3,5,8,9,7,6,4,2,0 };
	my_qsort(arr, 10, sizeof(int), cmp_int);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
}
void test2()
{
	struct Stu s[3] = { {"zhangsan",20},{"lisi",70},{"wangwu",33} };
	size_t num = sizeof(s) / sizeof(s[0]);
	my_qsort(s, num, sizeof(s[0]), cmp_structage);
}
void test3()
{
	struct Stu s[3] = { {"zhangsan",20},{"lisi",70},{"wangwu",33} };
	size_t num = sizeof(s) / sizeof(s[0]);
	my_qsort(s, num, sizeof(s[0]), cmp_structname);
}
int main()
{
	test1();
	test2();
	test3();
	return 0;
}